/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.voucherhave;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.services.front.voucher.VoucherService;
import shop.services.front.voucher.bean.Voucher;
import shop.services.front.voucherhave.VoucherhaveService;
import shop.services.front.voucherhave.bean.Voucherhave;
import shop.services.manage.system.bean.User;
import shop.services.manage.system.impl.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**   
 * @类名称：VoucherhaveController
 * @创建人：Wzl
 * @创建时间：2018-01-03 下午17:03:53      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/front/voucherhave/")
public class VoucherhaveController extends FrontBaseController<Voucherhave> {
    @Resource
    private VoucherhaveService voucherhaveService;
    @Resource
    private VoucherService voucherService;
    @Resource
    private UserService userService;
    @Override
    public Services<Voucherhave> getService() {
        return voucherhaveService;
    }


    /**
     * 查询用户礼卷列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("selectVoucherList")
    @ResponseBody
    public JSONResult selectVoucherList(HttpSession session,Voucherhave voucherhave){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String today="";
        User user=(User)session.getAttribute("userInfo");
        voucherhave.setUserID(user.getId());
        List<Voucherhave> voucherHaveList = voucherhaveService.selectVoucherList(voucherhave);
        if(voucherHaveList!=null&voucherHaveList.size()>0){
            for(Voucherhave voucherhaveInfo: voucherHaveList){
                Voucher voucher=new Voucher();
                voucher.setId(voucherhaveInfo.getVoucherID());
                voucher=voucherService.selectOne(voucher);
                if("cash".equals(voucher.getType())){
                    today=sdf.format(new Date());
                    if(today.compareTo(voucher.getEndDate())>0)
                        voucherHaveList.remove(voucherhaveInfo);
                }else if("birthday".equals(voucher.getType())){
                    today=sdf.format(new Date().getTime()-(voucher.getIndate()) * 24 * 60 * 60 * 1000);
                    if(today.compareTo(sdf.format(voucherhaveInfo.getCreateTime()))>0)
                        voucherHaveList.remove(voucherhaveInfo);
                }else{}
            }
        }
        JSONResult jsonResult = new JSONResult();
        jsonResult.setData(voucherHaveList);
        return jsonResult;
    }

    /**
     * 领取礼卷
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("insertVoucherHave")
    @ResponseBody
    public JSONResult insertVoucherHave(HttpSession session,Voucherhave voucherhave){
        String[] ids=voucherhave.getVoucherID().split("_");
        voucherhave.setVoucherID(ids[0]);
        JSONResult jsonResult = new JSONResult();
        User user=(User)session.getAttribute("userInfo");
        voucherhave.setUserID(user.getId());
        Voucherhave voucherhaveInfo=voucherhave;
        voucherhaveInfo=voucherhaveService.selectOne(voucherhaveInfo);
        if(voucherhaveInfo==null){
            voucherhaveService.insert(voucherhave);
            return jsonResult;
        }else{
            jsonResult.setSuccess(false);
            jsonResult.setMessage("您已领取过该打折卷，请勿重复领取！");
            return jsonResult;
        }
    }

    @Override
    public JSONResult deleteJson(HttpServletRequest request, Voucherhave voucherhave) throws Exception {
        return null;
    }
}
